package helper;

import java.util.Vector;

public class Library
{
	public Library()
	{

	}

	public String[] split(String delimiter, String target)
	{
		int begin = 0;
		int index = 0;
		int end = 0;
		Vector vtokens = new Vector();
		String token;
		
		while (begin < target.length())
		{
			index = target.indexOf(delimiter, begin);

			if (index >= 0)
			{
				end = index;
				token = target.substring(begin, end);
				vtokens.addElement(token);
				begin = index + delimiter.length();
			} 
			else if (begin < target.length())
			{
				end = index = target.length();
				token = target.substring(begin, end);
				vtokens.addElement(token);
				begin = index + delimiter.length();
			} 
			else
				break;
			
		}
		
		String tokens[] = new String[vtokens.size()];
		vtokens.copyInto(tokens);

		return tokens;
	}

	public boolean compareArrays(Object a[], Object b[])
	{
		if (a.length != b.length)
			return false;

		for (int i = 0; i < a.length; i++)
			if (!a[i].equals(b[i]))
				return false;

		return true;
	}

	public String urlEncode(String s)
	{
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < s.length(); i++)
		{
			switch (s.charAt(i))
			{
				case ' ':
					sb.append("+");
					break;
				case '+':
					sb.append("%2b");
					break;
				case '\'':
					sb.append("%27");
					break;
				case '<':
					sb.append("%3c");
					break;
				case '>':
					sb.append("%3e");
					break;
				case '#':
					sb.append("%23");
					break;
				case '%':
					sb.append("%25");
					break;
				case '{':
					sb.append("%7b");
					break;
				case '}':
					sb.append("%7d");
					break;
				case '\\':
					sb.append("%5c");
					break;
				case '^':
					sb.append("%5e");
					break;
				case '~':
					sb.append("%73");
					break;
				case '[':
					sb.append("%5b");
					break;
				case ']':
					sb.append("%5d");
					break;
				default:
					sb.append(s.charAt(i));
					break;
			}
		}
		
		return sb.toString();
	}
}
